import time
from fastapi import APIRouter
import datetime
import json
from DQN import DQN
from Instance_Generator import Instance_Generator
import warnings
warnings.filterwarnings("ignore")

router = APIRouter()


@router.post("/scheduling", summary="读取订单库，读取bom,读取ops, 返回宏观计划、详细计划、订单交付期表、原料计划表")
async def scheduling_api(data: dict):
    t0 = time.time()
    bom_dicts, J_num, order_data, Job_passtime_matrixs, Job_opnum_lst = Instance_Generator()
    d = DQN(bom_dicts, J_num, order_data, Job_passtime_matrixs, Job_opnum_lst)
    out = d.main()
    print('处理耗时：',time.time()-t0)
    return dict(code=200, message='success', result=out)

# @router.post("/update_order", summary="接收新订单，读取订单库， 更新保存到订单库，并返回新订单库，供接口调用")
# async def update_order_api(data: dict):
#     # with open('./data/test.json', 'r') as json_data:
#     #     order_update = json.load(json_data)
#     order_update = data['order_data']
#
#     with open('./data/order_data.json', 'r') as json_data:
#         order_data = json.load(json_data)
#     temp_lst = [dict01['订单编号'] for dict01 in order_data]
#     for dict02 in order_update:
#         a = dict02['数量']
#         a = int(a)
#         if isinstance (a,int) and a > 0:
#             continue
#         else:
#             return dict(code=403, message='fail', result='error in quantity')
#     for dict02 in order_update:
#         b = dict02['产品名称']
#         b = str(b)
#         if b in all_productins_types:
#             continue
#         else:
#             return dict(code=403, message='fail', result='error in product name')
#     for dict02 in order_update:
#         c = dict02['订单编号']
#         c = str(c)
#         if c in temp_lst:
#             return dict(code=403, message='fail', result='error in order number')
#         else:
#             temp_lst.append(c)
#
#     out = order_data + order_update
#     with open('./data/order_data.json', 'w') as file_obj:
#         json.dump(out, file_obj)
#     return dict(code=200, message='success', result=out)








@router.post("/init_order", summary="初始订单，供接口调用")
async def init_order_api(data: dict):
    with open('./data/init_order_data.json', 'r') as json_data:
        out = json.load(json_data)
    with open('./data/order_data.json', 'w') as file_obj:
        json.dump(out, file_obj)
    return dict(code=200, message='success', result=out)

@router.post("/init_taketime", summary="初始生产节拍，供接口调用")
async def init_taketime_api(data: dict):
    with open('./data/init_taketime_data.json', 'r') as json_data:
        out = json.load(json_data)
    with open('./data/taketime_data.json', 'w') as file_obj:
        json.dump(out, file_obj)
    return dict(code=200, message='success', result=out)

@router.post("/init_bom", summary="初始bom，供接口调用")
async def init_bom_api(data: dict):
    with open('./data/init_bom_data.json', 'r') as json_data:
        out = json.load(json_data)
    with open('./data/bom_data.json', 'w') as file_obj:
        json.dump(out, file_obj)
    return dict(code=200, message='success', result=out)

@router.post("/init_ops", summary="初始ops，供接口调用")
async def init_ops_api(data: dict):
    with open('./data/init_ops_data.json', 'r') as json_data:
        out = json.load(json_data)
    with open('./data/ops_data.json', 'w') as file_obj:
        json.dump(out, file_obj)
    return dict(code=200, message='success', result=out)

@router.post("/init_switch", summary="初始switch，供接口调用")
async def init_switch_api(data: dict):
    with open('./data/init_switch_data.json', 'r') as json_data:
        out = json.load(json_data)
    with open('./data/switch_data.json', 'w') as file_obj:
        json.dump(out, file_obj)
    return dict(code=200, message='success', result=out)


